#!/usr/bin/python3
# -*- coding: utf-8 -*-

from flask import Flask, redirect, url_for, request
import pyhtml
import pyjm

app = Flask(__name__)


@app.route('/')
def root():
    return redirect(url_for('pyjmlst1'), code=302)


@app.route("/pyhtmlt/")
def pyhtmlt():
    src = pyhtml.h("这是个标题")
    src = src + "\n" + pyhtml.p("这是个段落")
    src = src + "\n" + pyhtml.a("这是个指向主页的链接")
    src = src + "\n" + pyhtml.a("这是个指向非主页的链接", url="/pyjmt1/")
    src = src + "\n" + pyhtml.p("主页和非主页的区别是主页少了url参数")
    l = ['这是个无序列表', "使用", "pyhtml", "生成"]
    src = src + "\n" + pyhtml.lst(pyhtml.pli(l))
    l[0] = '这是个有序列表'
    src = src + "\n" + pyhtml.lst(pyhtml.pli(l), t="ol")
    src = src + "\n" + pyhtml.p("这是个特殊列表，它会把列表内的项目转化为链接")
    l = ["pyjmt1", "pyjmt2", "pyjmt3", "pyjmlst1"]
    src = src + "\n" + pyhtml.lst(pyhtml.pli(l, url=l), t="ol")
    src = src + "\n" + pyhtml.p("您也可以指定描述，像这样")
    d = ["jQuery Mobile", "jQuery Mobile页面",
         "jQuery Mobile对话框", "jQuery Mobile列表"]
    src = src + "\n" + pyhtml.lst(pyhtml.pli(d, url=l), t="ol")
    src = src + "\n" + pyhtml.p("这是张图片")
    src = src + "\n" + pyhtml.img("/static/pic.png")
    src = src + "\n" + \
        pyhtml.p("pyhtml引入了一个叫iv的组件，iv=iframe-video，提供了网页框架与视频，以下是iv-iframe")
    src = src + "\n" + pyhtml.iv("/pyjmt1/")
    src = src + "\n" + pyhtml.p("如需添加视频，请指定t=2（没素材，无法演示）")
    src = src + "\n" + pyhtml.p("iv和img都可以通过指定宽高改变大小：")
    src = src + "\n" + pyhtml.iv("/pyjmt1/", width="100%", height="100px")
    src = src + "\n" + pyhtml.p("还有audio，用法参照img（没素材，无法演示）")
    src = src + "\n" + pyhtml.p("另外本页的标题是通过title实现的，在最上面")
    src = src + "\n" + pyhtml.a("返回", url="/pyjmlst1/")
    src = pyhtml.main(src, pyhtml.title("PyHTML Test"))
    return src


@app.route("/pyjmt1/")
def pyjmt1():
    src = pyhtml.h("hello world")
    src = pyjm.pagemain(src)
    src = pyjm.page(src)
    src = pyhtml.main(src, pyjm.head())
    return src


@app.route("/pyjmt2/")
def pyjmt2():
    # 渲染head
    head = pyhtml.h("Test Page")
    head = pyjm.pagehead(head)
    # 渲染main
    src = pyhtml.h("hello world")
    src = src + "\n" + pyjm.a("这是个指向主页的内联按钮", inline='true')
    src = src + "\n" + pyjm.a("这是个指向主页的按钮")
    src = src + "\n" + pyhtml.p("内联设置只需要一个值，且与官方一致")
    gb = pyjm.a("这个按钮和") + "\n" + pyjm.a("这个按钮被组合了")
    src = src + "\n" + pyjm.group(gb)
    src = src + "\n" + pyjm.group(gb, t=2)
    src = src + "\n" + pyhtml.p("垂直只需设置t=2")
    src = src + "\n" + pyjm.backb("这是个返回按钮")
    src = pyjm.pagemain(src)
    # 渲染footer
    foot = pyjm.navbar(pyhtml.lst(pyhtml.p("页脚")))
    foot = pyjm.pagefoot(foot)
    # 渲染页面
    src = head + "\n" + src + "\n" + foot
    src = pyjm.page(src)
    # 渲染html
    src = pyhtml.main(src, pyjm.head())
    return src


@app.route("/pyjmt3/")
def pyjmt3():
    # 渲染head
    head = pyhtml.h("Test Page")
    head = pyjm.pagehead(head)
    # 渲染main
    src = pyhtml.h("hello world")
    src = pyjm.pagemain(src)
    # 渲染页面
    src = head + "\n" + src
    src = pyjm.page(src, dialog=True)
    # 渲染html
    src = pyhtml.main(src, pyjm.head())
    return src


l = ["pyhtmlt", "pyjmt1", "pyjmt2", "pyjmt3",
     "pyjmlst1", "pyjmlst2", "pyjmlst3", "pyjmlst4", "about"]
d = ["普通HTML", "jQuery Mobile", "jQuery Mobile页面", "jQuery Mobile对话框",
     "jQuery Mobile列表（无搜索框，无序）", "jQuery Mobile列表（带搜索框，有序）", "jQuery Mobile列表（带图）", "jQuery Mobile列表（小图双按钮）", "关于PyWAK（如果能写到你的软件里就更好了）"]
i = ["pic.png", "pic.png", "pic.png", "pic.png",
     "pic.png", "pic.png", "pic.png", "pic.png", "pic.png"]
si = ["1616.png", "1616.png", "1616.png", "1616.png",
      "1616.png", "1616.png", "1616.png", "1616.png", "1616.png"]


@app.route("/pyjmlst1/")
def pyjmlst1():
    # 渲染head
    head = pyhtml.h("列表测试")
    head = pyjm.pagehead(head)
    # 渲染main
    src = pyhtml.h("hello world")
    src = src + "\n" + pyjm.lst(pyhtml.pli(d, url=l))
    src = pyjm.pagemain(src)
    # 渲染页面
    src = head + "\n" + src
    src = pyjm.page(src)
    # 渲染html
    src = pyhtml.main(src, pyjm.head())
    return src


@app.route("/pyjmlst2/")
def pyjmlst2():
    # 渲染head
    head = pyhtml.h("列表测试")
    head = pyjm.pagehead(head)
    # 渲染main
    src = pyhtml.h("hello world")
    src = src + "\n" + pyjm.lst(pyhtml.pli(d, url=l), searchbox=True, t="ol")
    src = pyjm.pagemain(src)
    # 渲染页面
    src = head + "\n" + src
    src = pyjm.page(src)
    # 渲染html
    src = pyhtml.main(src, pyjm.head())
    return src


@app.route("/pyjmlst3/")
def pyjmlst3():
    # 渲染head
    head = pyhtml.h("列表测试")
    head = pyjm.pagehead(head)
    # 渲染main
    src = pyhtml.h("hello world")
    src = src + "\n" + \
        pyjm.lst(pyjm.pli(d, url=l, img=i), searchbox=False)
    src = pyjm.pagemain(src)
    # 渲染页面
    src = head + "\n" + src
    src = pyjm.page(src)
    # 渲染html
    src = pyhtml.main(src, pyjm.head())
    return src


@app.route("/pyjmlst4/")
def pyjmlst4():
    # 渲染head
    head = pyhtml.h("列表测试")
    head = pyjm.pagehead(head)
    # 渲染main
    src = pyhtml.h("hello world")
    src = src + "\n" + \
        pyjm.lst(pyjm.pli(d, url=l, simg=si, url2=l, src2=d), searchbox=False)
    src = pyjm.pagemain(src)
    # 渲染页面
    src = head + "\n" + src
    src = pyjm.page(src)
    # 渲染html
    src = pyhtml.main(src, pyjm.head())
    return src


@app.route("/about/")
def about():
    return pyjm.about()


app.run(host='0.0.0.0', debug=True)
